package ld.oauth.blog.controller.question;

import com.ld.anotation.Log;
import com.ld.common.ResultInfoFactory;
import ld.oauth.blog.model.bo.QuestionBo;
import ld.oauth.blog.model.bo.ReplyBo;
import ld.oauth.blog.model.qo.QuestionQo;
import ld.oauth.blog.model.vo.QuestionDetail;
import ld.oauth.blog.model.vo.ReplyVo;
import ld.oauth.blog.service.question.QuestionService;
import ld.oauth.blog.utils.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/question")
public class QuestionController {

    @Autowired
    private QuestionService questionService;

    /**
     * 新增或编辑问题
     *
     * @param questionBo
     * @return
     */
    @PostMapping("/insertOrUpdate")
    @Log(module = "问题模块",description = "新增或编辑问题")
    public ResultInfoFactory<Integer> insertOrUpdate(@RequestBody QuestionBo questionBo) {
        return ResultInfoFactory.success(questionService.insertOrUpdate(questionBo));
    }

    /**
     * 根据问题id查询文章详情
     *
     * @param id
     * @return
     */
    @GetMapping("/questionDetailById/{id}")
    @Log(module = "问题模块",description = "根据问题id查询文章详情")
    public ResultInfoFactory<QuestionDetail> questionDetailById(@PathVariable("id") Integer id) {
        return ResultInfoFactory.success(questionService.questionDetailById(id));
    }

    /**
     * 分页条件查询最热问题
     * @param questionQo
     * @return
     */
    @PostMapping("/pageQuery/hot")
    @Log(module = "问题模块",description = "分页条件查询最热问题")
    public ResultInfoFactory<PageResult<QuestionDetail>> hot(@RequestBody QuestionQo questionQo) {
        return ResultInfoFactory.success(questionService.hot(questionQo));
    }
    /**
     * 分页条件查询最新问题
     * @param questionQo
     * @return
     */
    @PostMapping("/pageQuery/new")
    @Log(module = "问题模块",description = "分页条件查询最新问题")
    public ResultInfoFactory<PageResult<QuestionDetail>> veryNew(@RequestBody QuestionQo questionQo) {
        return ResultInfoFactory.success(questionService.veryNew(questionQo));
    }

    /**
     * 分页条件查询等待回答问题
     * @param questionQo
     * @return
     */
    @PostMapping("/pageQuery/wait")
    @Log(module = "问题模块",description = "分页条件查询等待回答问题")
    public ResultInfoFactory<PageResult<QuestionDetail>> wait(@RequestBody QuestionQo questionQo) {
        return ResultInfoFactory.success(questionService.wait(questionQo));
    }

    /**
     * 更新问题浏览数
     *
     * @param id
     * @return
     */
    @RequestMapping("/updateQuestionViewCountById/{id}")
    @Log(module = "问题模块",description = "更新问题浏览数")
    public ResultInfoFactory<String> updateQuestionViewCountById(@PathVariable("id") Integer id) {
        questionService.updateArticleViewCountById(id);
        return ResultInfoFactory.success();
    }


    /**
     * 更新问题点赞数
     *
     * @param questionId
     * @param count
     * @return
     */
    @RequestMapping("/updateQuestionThumbById/{questionId}/{count}")
    @Log(module = "问题模块",description = "更新问题点赞数")
    public ResultInfoFactory<String> updateArticleThumbById(@PathVariable("questionId") Integer questionId, @PathVariable("count") Integer count) {
        questionService.updateArticleThumbById(questionId, count);
        return ResultInfoFactory.success();
    }

    /**
     * 新增或修改问题的回答
     * @param replyBo
     * @return
     */
    @RequestMapping("/insertOrUpdateQuestionReplay")
    @Log(module = "问题模块",description = "新增或修改问题的回答")
    public ResultInfoFactory<Integer> insertOrUpdateQuestionReplay(@RequestBody ReplyBo replyBo){
        return questionService.insertOrUpdateQuestionReplay(replyBo) == true ? ResultInfoFactory.success() : ResultInfoFactory.failed();
    }

    /**
     * 根据问题id获取问题的所有回答
     * @param questionId
     * @return
     */
    @RequestMapping("/getQuestionReplayById/{questionId}")
    @Log(module = "问题模块",description = "根据问题id获取问题的所有回答")
    public ResultInfoFactory<List<ReplyVo>> getQuestionReplayById(@PathVariable("questionId") Integer questionId){
        return ResultInfoFactory.success(questionService.getQuestionReplayById(questionId));
    }

    /**
     * 根据id删除回答
     * @param id
     * @return
     */
    @GetMapping("/deleteCommentById/{id}")
    @Log(module = "问题模块",description = "根据id删除回答")
    public ResultInfoFactory deleteCommentById(@PathVariable("id") Integer id) {
        return questionService.removeById(id) == true ? ResultInfoFactory.success() : ResultInfoFactory.failed();
    }

    /**
     * 根据标签id分页查询问题列表
     * @param questionQo
     * @return
     */
    @PostMapping("/pageQuestionByLabelId")
    @Log(module = "问题模块",description = "根据标签id分页查询问题列表")
    public ResultInfoFactory<PageResult<QuestionDetail>> queryQuestionByLabelId(@RequestBody QuestionQo questionQo){
        return ResultInfoFactory.success(questionService.queryQuestionByLabelId(questionQo));
    }



}
